var order = ['green', 'red', 'yellow', 'blue']

let memberId, orderF = 1, openId = '';
const getToken= require('../../utils/get_token.js');
const feedbackApi= require('../common/showToast.js');


const ckToken = async (that) => {//token确认
  let nTime = Math.floor(new Date().getTime() / 1000);
  let oldTime = wx.getStorageSync('ntime') || 0;
  if (nTime < oldTime)//现在的时间小于到期时间
  {
    thou(that);
  } else {
     await getToken.getToken();
    thou(that);
  }
}
const thou = (that) => {//token没有失效时执行的代码
  getAll(that);
  getOrder(that);
  if (openId == '') {
    getOpenId.getOpenId();
  }
}
const getAll =function(that){
  let params = [ 'shop/pingTuan/huoPinDdAndSqDdCount', {"memberId":memberId,"tuanIncome":0}]
  getToken.getData(function (res) {
    console.log(res.data)
    let data = res.data;
    that.data.sList[0].num=0;
    that.data.sList[1].num=data.needPay<=99?data.needPay:99;
    that.data.sList[2].num=data.needPin<=99?data.needPin:99;
    that.data.sList[3].num=data.waitShip<=99?data.waitShip:99;
    that.data.sList[4].num=data.hadShip<=99?data.hadShip:99;  
    that.data.sList[5].num=data.waitBack<=99?data.waitBack:99; 
    that.data.sList[6].num=data.failPin<=99?data.failPin:99; 
    that.data.sList[7].num=0;
    that.setData({
      sList: that.data.sList
    })
  }, ...params)
};
const getOrder = (that)=>{
  if (that.data.boolT)
  {
    that.data.boolT=false;
    that.setData({hidden: false})
    let params = ['shop/order/getOrderMemberViewList',{"tuanIncome":3,"state":that.data.currentTab,id:memberId,"pageNo":orderF,"pageSize":10}]
    getToken.getData(function (res) {
      console.log(res.data);
      setTimeout(function () { that.data.boolT = true;}, 300);
      let rows = res.data.rows;
      if (rows.length > 0) {
        for (let i = 0; i < rows.length; i++) {
          rows[i].tid = 1;
          rows[i].intTotalPrice = (rows[i].intTotalPrice / 100).toFixed(2);
          rows[i].intPrice = (rows[i].intPrice / 100).toFixed(2);
          if (rows[i].specialName != '') {
            rows[i].specialName = '规格:' + rows[i].specialName;
          }
          if (rows[i].isGroupBuy == 1) {
            rows[i].isShow = 1;
          } else {
            if (rows[i].lastNum >= 1) {
              rows[i].isShow = 2;
            }
            if (rows[i].status == '已发货' || rows[i].status == '交易成功') {
              rows[i].isShow = 3;
            }
          }
          rows[i].loading = false;
          rows[i].disabled = false;
          that.data.list.push(rows[i]);
        }
      }
      if (that.data.list.length > 0) {
        that.data.noShow = true;
      } else {
        that.data.noShow = false;
      }
      that.setData({
        hidden: true,
        noShow: that.data.noShow,
        list: that.data.list
      })
    }, ...params)
  }
}
Page({
  data: {
    currentTab: 0,
		hidden: true,
		boolT: true,
    s: { waitPay: 0, waitShou: 0, waitPing: 0, waitShip:0},
    noShow:true,
    list: [],
    sList:[
      {'name':'全部','num':0,"indx":0,"show":false},
      {'name':'待付款','num':0,"indx":1,"show":false},
      {'name':'待成团','num':0,"indx":2,"show":false},
      {'name':'待发货','num':0,"indx":3,"show":false},
      {'name':'待收货','num':0,"indx":4,"show":false},
      {'name':'待评价','num':0,"indx":5,"show":false},
      {'name':'退款退货','num':0,"indx":6,"show":false},
      {'name':'退款成功','num':0,"indx":7,"show":false}
    ]
  },
  onLoad: function (options) {
    var that = this;
    openId = wx.getStorageSync('openId') || '';
		memberId = wx.getStorageSync('memberId');
		orderF = 1;
    ckToken(that);
  },
  swichNav: function (e) {//点击
    var that = this;
    var tab = e.currentTarget.dataset.current;
    orderF = 1;
    that.setData({
      currentTab: tab,
      list: []
    })
      getOrder(that);
 

  },
  subClick: function (e) {//进入详情
    let that = this;
    let goodsname = e.currentTarget.dataset.goodsname;
    let memberordersn = e.currentTarget.dataset.memberordersn;
    let tuanlistid = e.currentTarget.dataset.tuanlistid;
    let god = e.currentTarget.dataset.god;
    let gimg ='https://www.globalhexi.com/'+e.currentTarget.dataset.gimg;
    let ado=e.currentTarget.dataset.addordersn;
    wx.setStorageSync('wimg', gimg);
    if (goodsname != "充值订单") {
      if (that.data.boolT) {
        that.data.boolT = false;
        wx.navigateTo({
          url: `../p_orderD/p_orderD?id=${memberordersn}&tuanId=${tuanlistid}&aordersn=${ado}&god=${god}` ,
          success: function () {
            setTimeout(function () {that.data.boolT = true;}, 500)
          }
        })
      }
    }
  },
  goLoginst:function(e){
    let that = this;
    let id = e.currentTarget.dataset.id;
    let gimg ='https://www.globalhexi.com/'+e.currentTarget.dataset.gimg;
    wx.setStorageSync('wimg', gimg);
    if (that.data.boolT) {
      that.data.boolT = false;
      wx.navigateTo({
        url: '../logistics/logistics?id=' + id,
        success: function () {
          setTimeout(function () {
            that.data.boolT = true;
          }, 500)
        }
      })
    }
  },
	sure:function(e){
		var that = this;
		orderF = 1;
		wx.showModal({
			title: '提示',
			content: '是否确认收货',
			success: function (res) {
				if (res.confirm) {
          let params = ['shop/order/yesTake', {"orderSn": e.target.dataset.id }]
          getToken.getData(function (res) {
            if (res.data.flag == "true") {
              that.setData({
                list: []
              })
              orderF = 1;
              getAll(that);
              getOrder(that);
            } else {
              feedbackApi.showToast({ title: res.data.message, mask: false })
            }
          }, ...params)
				} 
			}
		})
	},
	pay:function(e){//去付款
		var that = this;
		var orderSn = e.target.dataset.id;//商品id
		var stype = e.target.dataset.stype;//商品类型
		for (let i = 0; i < that.data.list.length;i++)
		{
			if (orderSn == that.data.list[i].orderSn){
				that.data.list[i].loading=true;
				that.data.list[i].disabled = true;
				that.setData({
					list:that.data.list
				})
			}
		}
		if (that.data.boolT){
			that.data.boolT = false;
      if (stype == "CZDD") {
        var data = '{"openId":"' + openId + '","orderSn":"' + orderSn + '","memberId":"' + memberId + '","isMini":"mini"}';
        var Url = 'shop/pay/payCzdd';
      } else {
        var data = '{"openId":"' + openId + '","orderSn":"' + orderSn + '","memberId":"' + memberId + '","isMini":"mini"}';
        var Url = 'shop/pay/createOrderForHexiSMtoPay';
      }
      let params = [ 'shop', Url, data]
      getToken.pay(function (res) {
        console.log(res.data);
        if (stype == "CZDD") {
          var result = res.data.content;
        } else {
          var result = res.data;
        }

        if (res.data.flag) {
          wx.requestPayment({
            'timeStamp': result.timeStamp,
            'nonceStr': result.nonceStr,
            'package': result.package,
            'signType': result.signType,
            'paySign': result.paySign,
            'success': function (res) {
              that.setData({list: []})
              orderF = 1;
              getAll(that);
              getOrder(that);
            },
            'fail': function (res) {
              console.log(res)
            }
          })
        } else {
          feedbackApi.showToast({ title: res.data.message, mask: false })//调用 showToast
        }
        that.data.boolT = true;
        for (let j = 0; j < that.data.list.length; j++) {
          if (orderSn == that.data.list[j].orderSn) {
            that.data.list[j].loading = false;
            that.data.list[j].disabled = false;
            that.setData({
              list: that.data.list
            })
          }
        }
      }, ...params)
		}
		
	},
//	页面滑动到底部
	onReachBottom: function () {
    let that = this;
    orderF++;

    getOrder(that);
	},
	onPullDownRefresh: function () {//下拉刷新
	var that=this;
		wx.showNavigationBarLoading() //在标题栏中显示加载
			that.setData({list: []})
			orderF = 1;
    getAll(that);
    let params = ['shop/order/getOrderMemberViewList',{"tuanIncome":3,"state":that.data.currentTab,id:memberId,"pageNo":orderF,"pageSize":10}]
    getToken.getData(function (res) {
      console.log(res.data);
      let rows = res.data.rows;
      if (rows.length > 0) {
        for (let i = 0; i < rows.length; i++) {
          rows[i].tid = 1;
          rows[i].intTotalPrice = (rows[i].intTotalPrice / 100).toFixed(2);
          rows[i].intPrice = (rows[i].intPrice / 100).toFixed(2);
          if (rows[i].specialName != '') {
            rows[i].specialName = '规格:' + rows[i].specialName;
          }
          if (rows[i].isGroupBuy == 1) {
            rows[i].isShow = 1;
          } else {
            if (rows[i].lastNum >= 1) {
              rows[i].isShow = 2;
            }
            if (rows[i].status == '已发货' || rows[i].status == '交易成功') {
              rows[i].isShow = 3;
            }
          }
          rows[i].loading = false;
          rows[i].disabled = false;
          that.data.list.push(rows[i]);
        }
      }
      if (that.data.list.length > 0) {
        that.data.noShow = true;
      } else {
        that.data.noShow = false;
      }
      that.setData({
        noShow: that.data.noShow,
        list: that.data.list
      })
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新	
    }, ...params)
	}
})
